// pages/passengers/passengerDe.js
var Tool = require("../../utils/ztool.js");
var tool = new Tool();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IDD: '',
    list: [],
    //订单错误标识
    order_error: false,
    //弹出框标识
    hiddenmodalput: true,
    startTime: '',
    endTime: '',
    leftmoney: '',
    payPrice: '',
    driverlist: [],
    isregularbus: false,
  },

  callPhone: function (e) {
    if (e.currentTarget.id == "") {
      wx.showToast({
        title: '号码为空',
        icon: 'none',
      });
      return;
    }
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id //仅为示例，并非真实的电话号码
    });
  },

  Paynow: function (e) {
    this.showError('微信支付尚未开通')
    return
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },

  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },

  confirm: function () {
    console.log(this.data.payPrice)
    paying(this)
    this.setData({
      hiddenmodalput: true
    });
  },

  inputPay: function (e) {
    var pri = e.detail.value
    this.data.payPrice = pri;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    var IDD1 = options.IDD;
    this.setData({
      IDD: IDD1,

    });
    if (!IDD1) {
      this.setData({
        order_error: true
      });
    } else {
      //请求加载订单数据
      getServiceData(this);
      //加载车辆和司机安排
      this.getdriverandcar();
    }
  },

  //获取订单有关司机和车辆安排
  getdriverandcar: function () {
    var that = this
    that.setData({
      driverlist: [],
    });
    console.log(that.data.driverlist.length)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/DriverPerson/GetDriverCarInfo.ashx',
      data: {
        IDD: that.data.IDD,
      },
      method: 'GET',
      success: function (res) {

        if (res.statusCode == 404) {
          //接口加载失败
          console.log('加载接口错误');
          //显示错误信息
          that.showError('订单信息接口请求失败')
          that.setData({
            loadError: true
          });
          return
        }

        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length > 0) {

          if (data.data[0].bcTime != '') {
            for (var i = 0; i < data.data.length; i++) {
              data.data[i].bcTime = data.data[i].bcTime.substring(5, 16)
            }
            that.setData({
              // driverlist: that.data.driverlist.concat(data.data),
              isregularbus: true
            });
          }

          that.setData({
            // driverlist: that.data.driverlist.concat(data.data),
            driverlist: data.data
          });

          console.log(that.data.driverlist)
        }
      },
      fail: function () {
        console.log('加载错误');
        wx.hideToast();
        that.setData({
          loadError: true
        });
      }
    })
  },

  calldriver: function (e) {
    console.log(e)
    var phoneNumber = e.currentTarget.dataset.text
    console.log(phoneNumber)
    if (phoneNumber == "") {
      wx.showToast({
        title: '号码为空',
        icon: 'none',
      });
      return;
    }

    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },


  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },

  locateDriver: function (e) {
    var that = this
    var dateEnd = e.currentTarget.dataset.idx
    var isopen = e.currentTarget.dataset.open
    console.log(dateEnd)
    var time1 = util.formatTime1(new Date())
    console.log(time1)
    if (dateEnd < time1) {
      that.showError('订单已过期')
      return
    }
    if (isopen == 0) {
      that.showError('该司机尚未开启定位功能')
      return
    }
    var driverID = e.currentTarget.id
    wx.navigateTo({
      url: '../travelAgency/locate?IDD=' + that.data.IDD + '&DriverID=' + driverID + '&retype=3',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  locateGuider: function (e) {
    var that = this
    var dateEnd = e.currentTarget.dataset.idx
    var isopen = e.currentTarget.dataset.open
    console.log(dateEnd)
    var time1 = util.formatTime1(new Date())
    console.log(time1)
    if (dateEnd < time1) {
      that.showError('订单已过期')
      return
    }
    if (isopen == 0) {
      that.showError('该导游尚未开启定位功能')
      return
    }
    var driverID = e.currentTarget.id
    wx.navigateTo({
      url: '../travelAgency/locate?IDD=' + that.data.IDD + '&DriverID=' + driverID + '&retype=4',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  focusRegularBus: function () {

    // wx.showActionSheet({
    //   itemList: ['关注本次行程', '关注本车', '关注其他班次'],
    //   success: function (res) {
    //     console.log(res.tapIndex)
    //   },
    //   fail: function (res) {
    //     console.log(res.errMsg)
    //   }
    // })

    var that = this
    wx.navigateTo({
      url: 'focusRegularBus/focusRegularBus?idd=' + that.data.IDD,
    })
  }
})

function getServiceData(that) {
  wx.showToast({
    title: '正在加载...',
    icon: 'loading',
    duration: 10000
  });
  var userInfo = wx.getStorageSync('userInfo');
  wx.request({
    url: 'https://www.lanshuicar.com/Server/CustS/OrderInfo.ashx',
    data: {
      IDD: that.data.IDD,
      methd: '2',
      Tel: userInfo.cTel
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      // success

      var data = tool.getRegularJson(res.data);
      var code = data.code;
      console.log(res);
      if (code == 0) {
        that.setData({
          list: data.data[0],
          startTime: data.data[0].UseCarStartTime.substring(0, 16),
          endTime: data.data[0].UseCarEndTime.substring(0, 16),
        });
        var a = that.data.list.cPrice;
        var b = that.data.list.cPricePaid;
        that.setData({
          leftmoney: a - b
        });

        console.log(that.data.leftmoney);
        console.log(a - b)

        console.log(that.data.list)
      } else {
        that.setData({
          order_error: true
        });

      }
    },
    fail: function () {
      // fail
      that.setData({
        order_error: true
      });
    },
    complete: function () {
      // complete
      wx.hideToast();
      wx.stopPullDownRefresh();
    }
  })
}

function paying(that) {
  if (that.data.payPrice > that.data.leftmoney) {
    wx.showToast({
      title: '付款大于应付金额',
    })
    return
  }
  var userinfo = wx.getStorageSync('userInfo');
  console.log(userinfo)
  wx.showToast({
    title: '正在加载...',
    icon: 'loading',
    duration: 10000
  });
  wx.request({
    url: 'https://www.lanshuicar.com/Server/AliPay.ashx',
    data: {
      Tel: userinfo.cTel,
      Type: '2',
      UType: '5',
      Price: that.data.payPrice,
      IDD: that.data.list.IDD

    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      // success
      var data = tool.getRegularJson(res.data);
      var code = data.code;
      console.log(res);
      if (code == 0) {
        wx.showToast({
          title: '支付成功',
        });
        getServiceData(that);
      } else {

      }
    },
    fail: function () {

    },
    complete: function () {
      // complete
      wx.hideToast();
      wx.stopPullDownRefresh();
    }
  })
}